(function ($) {

    var initTable = function () {
        $("#data-table").bootstrapTable({
            url: "/api/menus",
            method: "GET",
            cache: false,
            debug: true,
            striped: true,
            pagination: true,
            sidePagination: "server",
            pageNumber: 0,
            pageSize: 10,
            pageList: [10, 25, 50],
            uniqueId: "id",
            queryParams: function (params) {
                return {
                    size: params.limit,
                    page: params.offset / params.limit + 1,
                    param: ""
                };
            },
            responseHandler: function (data) {
                return {
                    total: data.totalElements,
                    rows: data.content
                };
            },
            columns: [
                {
                    title: "名称",
                    field: "name"
                }
                , {
                    title: "类型",
                    field: "type"
                }
                , {
                    title: "url",
                    field: "url"
                }
               /* , {
                    title: "父菜单",
                    field: "parent",
                    formatter: function (value, row, index) {
                        console.log(value);
                    }
                }*/
                , {
                    field: "id",
                    title: "操作",
                    align: 'center',
                    valign: 'middle',
                    formatter: actionFormatter
                }
            ],
            onLoadError: function () {

            }
            ,onPostBody: function () {
                $(".edit").bind("click", function() {
                    window.location.href="/html/admin/menu/edit/" + $(this).data("id");
                });
                $(".delete").bind("click", function() {
                    var id = $(this).data("id");
                    parent.layer.confirm('确认删除？', {
                        btn: ['确定','取消']
                    }, function(){
                        $.post("/api/menus/" + id, {
                            _method: "delete"
                        }, function(data) {
                            if (data.code === 200 || data.status === 200) {
                                parent.layer.msg("成功");
                                $("#data-table").bootstrapTable('refresh');
                            } else {
                                parent.layer.msg("失败, 请联系管理员");
                            }


                        });
                    }, function(){
                    });
                });

                return false
            }
        });
    };

    var actionFormatter = function (value, row, index) {
        var text = '<div class="tpl-table-black-operation">';
        text += '<a href="javascript: void(0);" data-id="' + value + '" class="edit"> <i class="am-icon-pencil"></i> 编辑 </a>';
        text += '<a href="javascript: void(0);" data-id="' + value + '" class="delete tpl-table-black-operation-del ml10"> <i class="am-icon-trash"></i> 删除 </a>';

        text += '</div>';
        return text;
    };

    var initListener = function() {
        $("#add").bind("click", function() {
            window.location.href="/html/admin/menu/add";
        });
    };

    initTable();
    initListener();
})(jQuery);